﻿using BusinessLayer;
using BusinessLayer.VDataEntities;
using CustomerServiceCenter.Auth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace CustomerServiceCenter.Helper
{
    public class WechatAuthenticationAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            VPromoter user = null;
            string guuid = HttpContext.Current.Request.QueryString["id"];
            long clientID = 0;
            if (guuid != null || !string.IsNullOrWhiteSpace(guuid))
            {

                guuid = guuid.ToString();
                clientID = ClientBusiness.GetClient(guuid).ID;
            }
            else
            {
                HttpCookie client_Cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName + "clientid"];
                if (client_Cookie == null)
                {
                    HttpContext.Current.Response.Redirect("~/Error/ErrorURL");
                }
                FormsAuthenticationTicket tit_client = FormsAuthentication.Decrypt(client_Cookie.Value);
                long.TryParse(tit_client.UserData.ToString(), out clientID);
            }

            LogHelper.Info("WeChatAuthentication:" + clientID + "-->" + guuid);


#if (DEBUG)
            {
                user = new VPromoter
                {
                    ChineseName = "kingkang",
                    ClientID = 6
                };
            }
#else
            //企业号
            user = Authenticate.CheckWeiXinUser(clientID);

            //服务号
            //user = AuthenticateService.CheckWeiXinUser(clientID);

#endif
            LogHelper.Info("验证后获取到的用户信息：\r\n" + user.ToString());
            if (user == null || user.ChineseName == null)
            {
                HttpContext.Current.Response.Redirect("~/Error/AuthFail");
            }
            else
            {
                VPromoter promoter = HttpContext.Current.Session["PromoterInfo"] as VPromoter;
                if (promoter != null)
                {
                    LogHelper.Info(promoter.ToString());
                }

                if (promoter == null || promoter.ClientID != clientID)
                {
                    //LogHelper.Info("session设置43");
                    //LogHelper.Info("session设置45");
                    //LogHelper.Info("从session中获取Client：" + clientID);
                    promoter = PromoterBusiness.ValidatePromoter(user.ChineseName, clientID);
                    //LogHelper.Info("session设置55");
                    if (promoter == null)
                    {
                        //服务号
                        //PromoterBusiness.AddPromoter(new VEndUser { ClientID = user.ClientID, Name = user.ChineseName, openid = user.openid });
                        //promoter = PromoterBusiness.ValidatePromoter(user.ChineseName, user.ClientID, user.openid);
                        
                        //企业号
                        PromoterBusiness.AddPromoter(new VEndUser { ClientID = user.ClientID, Name = user.ChineseName });
                        promoter = PromoterBusiness.ValidatePromoter(user.ChineseName, user.ClientID);
                    }
                    //LogHelper.Info("添加session：" + promoter.ToString());
                    HttpContext.Current.Session["PromoterInfo"] = promoter;
                }
            }
        }
    }
}